package com.codedancer.campus.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.manager.service.ICommodityExamineRecordService;
import com.codedancer.campus.manager.service.bo.commodity.CommodityExamineRecordPageSearchBo;
import com.codedancer.campus.manager.service.vo.commodity.CommodityExamineRecordVo;
import com.codedancer.campus.mapper.CommodityExamineRecordMapper;
import com.codedancer.campus.po.CommodityExamineRecord;
import com.codedancer.campus.qo.commodity.CommodityExamineRecordPageSearchQo;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class CommodityExamineRecordServiceImpl implements ICommodityExamineRecordService {

    @Autowired
    private CommodityExamineRecordMapper commodityExamineRecordMapper;

    /**
     * 分页查询商品审核记录
     *
     * @param commodityExamineRecordPageSearchBo
     * @return
     */
    @Override
    public IPage<CommodityExamineRecordVo> page(
        CommodityExamineRecordPageSearchBo commodityExamineRecordPageSearchBo) {
        CommodityExamineRecordPageSearchQo commodityExamineRecordPageSearchQo = BeanUtil
            .copyProperties(commodityExamineRecordPageSearchBo,
                CommodityExamineRecordPageSearchQo.class);
        IPage<CommodityExamineRecord> commodityExamineRecordIPage = commodityExamineRecordMapper
            .page(new Page<CommodityExamineRecord>(commodityExamineRecordPageSearchBo.getPage(),
                commodityExamineRecordPageSearchBo.getSize()), commodityExamineRecordPageSearchQo);
        List<CommodityExamineRecordVo> commodityExamineRecordVos;
        IPage<CommodityExamineRecordVo> commodityExamineRecordVoIPage = new Page<>();
        commodityExamineRecordVos = commodityExamineRecordIPage.getRecords().stream()
            .map(commodityExamineRecord -> {
                CommodityExamineRecordVo commodityExamineRecordVo = BeanUtil
                    .copyProperties(commodityExamineRecord, CommodityExamineRecordVo.class);
                commodityExamineRecordVo
                    .setExamineAdminName(commodityExamineRecord.getAdmin().getLoginName());
                return commodityExamineRecordVo;
            }).collect(Collectors.toList());
        commodityExamineRecordVoIPage.setRecords(commodityExamineRecordVos);
        commodityExamineRecordVoIPage.setTotal(commodityExamineRecordIPage.getTotal());
        return commodityExamineRecordVoIPage;
    }
}
